package x.amadeus.admin.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.itcast.commons.CommonUtils;

import x.amadeus.admin.domain.Admin;
import x.amadeus.admin.service.AdminService;

import x.amadeus.student.domain.MyException;
import x.amadeus.utils.Result;

@Controller
@RequestMapping("/admin")
public class AdminController {
	
	@Resource(name="adminService")
	private AdminService adminService;
	

	

	@RequestMapping("/test")
	@ResponseBody
	public Result test(HttpServletRequest request, HttpServletResponse response)
	{
		Result result = new Result();
		result.setCode(1);
		result.setMessage("欢迎管理员");
		result.setData(null);
		
		return result;
	}
	
	
	@RequestMapping("/login")
	@ResponseBody
	public Result adminLogin(HttpServletRequest request, HttpServletResponse response)
	{	
		Admin form = CommonUtils.toBean(request.getParameterMap(), Admin.class);
		Result result = new Result();
		Admin admin = null;
		try {
			admin = adminService.adminLogin(form);
			
			request.getSession().setAttribute("session_admin", admin);
			
			result.setCode(1);
			result.setMessage("登陆成功");
			result.setData(admin);
		} catch (MyException e) {
			result.setCode(0);
			result.setMessage(e.getMessage());
			result.setData(form);
			return result;
		}
		return result;
	}
	
}
